
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const getQueryVal = (str,queryName) => {
  let reg = new RegExp("(^|&)" + queryName + "=([^&]*)(&|$)", "i");
  let r = str.match(reg);
  if ( r != null ){
    return decodeURI(r[2]);
  }else{
    return null;
  }
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const config = {
  'contentType': 'application/x-www-form-urlencoded',
}

const configByJson = {
  'contentType': 'application/json',
}

const configByUpLoadFile = {
  'contentType': 'multipart/form-data',
}

const getCurPage = () => {
	let path = getCurrentPages();
	let cpaths = path[path.length-1];
	return cpaths
}

const getUrlVal = (url) => {
	var _url = '';
	if(url) {
		_url = url;
	} else {
		_url = window.location.href;
	}
	var Request = new Object();
	if (_url.indexOf("?") != -1) {　
		var urlindex = _url.indexOf("?")+1　
	    var str = _url.substr(urlindex)
	        　　 var strs = str.split("&");　　
	    for (var i = 0; i < strs.length; i++){　　
	        var tarGets = strs[i].split("=")
	        Request[tarGets[0]] = unescape(tarGets[1]);　　
	    }
	    return Request
	} else {
	    return Request
	}
}

const getQueryString = (name) => {
	let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
	let regRewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i')
	let r = window.location.search.substr(1).match(reg)
	let q = window.location.pathname.substr(1).match(regRewrite)
	let h = window.location.hash.substr(1).match(reg)
	// console.log(r,q,h)
	if(r != null) {
		return unescape(r[2])
	} else if(q != null) {
		return unescape(q[2])
	}else if(h != null){
		return unescape(h[2])
	} else {
		return null
	}
}

// 处理富文本里的图片宽度自适应
const formatRichText = (html) => {
	let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
	return newContent;
}

module.exports = {
  formatTime: formatTime,
  getQueryVal:getQueryVal,
  config: config,
  configByJson: configByJson,
  configByUpLoadFile: configByUpLoadFile,
  getCurPage:getCurPage,
  getQueryString:getQueryString,
  getUrlVal:getUrlVal,
  formatRichText:formatRichText,
}
